import { controller, httpGet as get, httpPost as post } from "inversify-express-utils";
import { UserServices } from "./user.services";
import { inject } from 'inversify'
import { Request, Response } from "express";
import { JWT } from '../../jwt'

/**
 * ## User 模块控制器
 * - 根路径：/user
 */

@controller('/user')
export class User {

  constructor(
    @inject(UserServices) private readonly user: UserServices
  ) {}

  /** 用户登录接口 */
  @get('/login', JWT.middleware())
  public async login(req: Request, res: Response) {
    console.log(req.user.id) // 获取用户信息
    let result = await this.user.login()
    res.send(result)
  }

  /** 注册账号接口 */
  @post('/signup')
  public async signup(req: Request, res: Response) {
    let result = await this.user.signup(req.body)
    res.send(result)
  }
}

/** 扩充 Request 与 User 类型 */
declare global {
  namespace Express {
    export interface Request {
      user?: User;
    }

    export interface User {
      id: number;
      username: string;
      password: string;
    }
  }
}